//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

package uxgenes.ext.ads;


import uxgenes.core.Logger;
import android.content.Context;
import android.os.Handler;
import android.view.View;


// #ifdef ADMOB
import com.google.ads.*;
// #endif ADMOB


//
// Ad Mob Banner
//
public final class AdMobBanner implements uxgenes.ext.IExtension
{
// variables //
	
	private final static String	TAG = "AdMobBanner";

	// #ifdef ADMOB
	private AdView 			_adView = null;
	private int				_refreshRate = 60;
	// #endif ADMOB
	
	
// methods //
	
	// constructor
	public AdMobBanner (Context ctx)
	{
	}

	
	
// Events //

	// OnAdded
	public final void OnAdded (uxgenes.core.EngineContext eng) {}
	
	
	// OnRemoved
	public final void OnRemoved () 	{}
	
	
	// OnInit
	public final void OnInit () {}
	
	
	// OnDestroy
	public final void OnDestroy ()
	{
		// #ifdef ADMOB
		if ( _adView != null ) {
			_adView.destroy();
			_adView = null;
		}
		if ( _bannerHandler != null ) {
			_bannerHandler.removeCallbacks( _bannerUpdater );
			_bannerHandler = null;
		}
		// #endif ADMOB
	}
	
	
	// OnResume
	public final void OnResume () {}

	
	// OnStart
	public final void OnStart ()
	{
		// #ifdef ADMOB
		if ( _bannerHandler != null ) {
			_bannerHandler.post( _bannerUpdater );
		}
		// #endif ADMOB
	}
	
	
	// OnPause
	public final void OnPause () {}


	// OnStop
	public final void OnStop ()
	{
		if ( _bannerHandler != null ) {
			_bannerHandler.removeCallbacks( _bannerUpdater );
		}
	}

	
	
// Baner //
	
	// #ifdef ADMOB
	private Handler			_bannerHandler = null;
	private final Runnable	_bannerUpdater = new Runnable ()
	{
		public void run()
		{
			if ( _adView != null && _bannerHandler != null ) {
			    _adView.loadAd( new AdRequest() );
				_bannerHandler.postDelayed( _bannerUpdater, _refreshRate * 1000 );
			}
		}
	};
	// #endif ADMOB
	
	
	// InitBanner
	public final void InitBanner (View root, int adViewId)
	{
		// #ifdef ADMOB
	    _adView = (AdView) root.findViewById( adViewId );
	    
	    if ( _bannerHandler != null ) {
	    	_bannerHandler.removeCallbacks( _bannerUpdater );
	    	_bannerHandler = null;
	    }
	    _bannerHandler = new Handler();
	    _bannerHandler.post( _bannerUpdater );
	    /* #else ADMOB
	    Logger.W( TAG, "AdMob not suported" );
		// #endif ADMOB */
	}
	
	
	// SetBannerRefreshRate
	public final void SetBannerRefreshRate (int seconds)
	{
		// #ifdef ADMOB
		_refreshRate = Math.max( seconds, 30 );
		// #endif ADMOB
	}


	// RefreshBanner
	public final void RefreshBanner ()
	{
		// #ifdef ADMOB
		if ( _adView != null ) {
			_adView.loadAd( new AdRequest() );
		}
		// #endif ADMOB
	}

	
	// IsBannerInitialized
	public final boolean IsBannerInitialized ()
	{
		// #ifdef ADMOB
		return _adView != null;
		/* #else ADMOB
		return false;
		// #endif ADMOB */
	}

	
	// IsBannerShown
	public final boolean IsBannerShown ()
	{
		// #ifdef ADMOB
		return _adView.isShown();
		/* #else ADMOB
		return false;
		// #endif ADMOB */
	}

	
	// IsBannerLoaded
	public final boolean IsBannerLoaded ()
	{
		// #ifdef ADMOB
		return true;
		/* #else ADMOB
		return false;
		// #endif ADMOB */
	}
	
}
